package com.example.demo.actuator.endpoint;

import com.example.demo.gateway.entity.GatewayRoute;
import com.example.demo.gateway.repository.MySQLRouteDefinitionRepository;
import com.example.demo.gateway.service.GatewayRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
@Endpoint(id = "route")
public class GatewayRouteEndpoint {
    @Autowired
    private MySQLRouteDefinitionRepository mySQLRouteDefinitionRepository;
    @Autowired
    private GatewayRouteService gatewayRouteService;

    @ReadOperation
    public Flux<RouteDefinition> list() {
        return  mySQLRouteDefinitionRepository.getRouteDefinitions();
    }


    @WriteOperation
    public Mono<Void> refresh() {
        return gatewayRouteService.refresh();
    }
}
